package com.example.jiaju.ui.home;

import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.jiaju.DataApplication;
import com.example.jiaju.R;
import com.example.jiaju.adapter.FurnitureRecommendAdapter;
import com.example.jiaju.adapter.NewHouseAdapter;
import com.example.jiaju.api.FurnitureApi;
import com.example.jiaju.api.HouseApi;
import com.example.jiaju.entity.Furniture;
import com.example.jiaju.entity.House;
import com.example.jiaju.utils.MyItemDecoration;
import com.example.jiaju.utils.RetrofitUtils;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class FurnitureHomeFragment extends Fragment {

    private List<Furniture.PageDTO.ListDTO> list;
    private RecyclerView recyclerView;
    private FurnitureRecommendAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_furniture_home, container, false);

        //网络请求三步走
        //1.获取到API接口
        FurnitureApi Api= RetrofitUtils.getRetrofit(DataApplication.HTTP_URL).create(FurnitureApi.class);
        //2.获取到实体类
        Call<Furniture> call=Api.getAllFurniture(1,5);
        //3.消息请求队列，取出数据
        call.enqueue(new Callback<Furniture>() {
            @SuppressLint("MissingInflatedId")
            @Override
            public void onResponse(Call<Furniture> call, Response<Furniture> response) {
                Furniture furniture=response.body();
                list=furniture.page.list;
                recyclerView=view.findViewById(R.id.furniture_home_recyclerview);
                adapter=new FurnitureRecommendAdapter(list,getActivity());
                recyclerView.setAdapter(adapter);
                recyclerView.addItemDecoration(new MyItemDecoration(14));
                recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
            }

            @Override
            public void onFailure(Call<Furniture> call, Throwable t) {

            }
        });

        return view;
    }
}